package com.twinkle.controller;

import com.twinkle.bean.Contacts;
import com.twinkle.bean.UserInfo;
import com.twinkle.service.ContactsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/")
public class ContactsController {

    @Autowired
    private ContactsService contactsService;

    @RequestMapping("/selectLike")
    public ModelAndView selectSContactLike(HttpServletRequest httpServletRequest, HttpSession session) {
        String search_name = httpServletRequest.getParameter("search_name");
        UserInfo userInfo = (UserInfo) session.getAttribute("para");
        session.setAttribute("listAll", contactsService.selectSContactLike(userInfo.getUserinfo_id(),
                search_name));
        System.out.println("==== SelectLike -> \"" + search_name + "\"");
        return new ModelAndView("redirect:/main.jsp");
    }

    @RequestMapping("/selectAll")
    public ModelAndView selectSContact(HttpSession session) {
        System.out.println("==== Select All");
        UserInfo userInfo = (UserInfo) session.getAttribute("para");
        session.removeAttribute("listAll");
        session.setAttribute("listAll", contactsService.selectSContact(userInfo.getUserinfo_id()));
        System.out.println("==== Select All Success");
        return new ModelAndView("redirect:/main.jsp");
    }

    @RequestMapping("/insert")
    public ModelAndView insertSContacts(HttpServletRequest httpServletRequest, HttpSession session) {
        System.out.println("\n==== Insert Contacts");
        Contacts contacts = new Contacts();
        UserInfo userInfo = (UserInfo) session.getAttribute("para");
        contacts.setContacts_id(Integer.parseInt(httpServletRequest.getParameter("co_id")));
        contacts.setContacts_name(httpServletRequest.getParameter("co_name"));
        contacts.setContacts_phone(httpServletRequest.getParameter("co_phone"));
        contacts.setContacts_email(httpServletRequest.getParameter("co_email"));
        System.out.println("== " + contacts.getContacts_id() + "," + contacts.getContacts_name() + ","
                + contacts.getContacts_phone() + "," + contacts.getContacts_email());
        boolean isInsert = contactsService.insertSContacts(contacts);
        if (isInsert) {
            System.out.println("==== Insert Success\n==== " + contacts.toString());
            session.setAttribute("listAll", contactsService.selectSContact(userInfo.getUserinfo_id()));
        } else {
            System.out.println("==== Insert Failure");
        }
        return new ModelAndView("redirect:/main.jsp");
    }

    @RequestMapping("/delete/{c_id}")
    public ModelAndView deleteSContacts(@PathVariable Integer c_id, HttpSession session) {
        System.out.println("\n==== Delete Contacts");
        UserInfo userInfo = (UserInfo) session.getAttribute("para");
        boolean isDelete = contactsService.deleteSContacts(c_id);
        if (isDelete) {
            System.out.println("==== Delete Success\n==== " + c_id);
            session.setAttribute("listAll", contactsService.selectSContact(userInfo.getUserinfo_id()));
        } else {
            System.out.println("==== Delete Failure");
        }
        return new ModelAndView("redirect:/main.jsp");
    }

    @RequestMapping("/update")
    public ModelAndView updateSContacts(HttpServletRequest httpServletRequest) {
        Contacts contacts = new Contacts();
        contacts.setContacts_id(Integer.parseInt(httpServletRequest.getParameter("up_id")));
        contacts.setContacts_name(httpServletRequest.getParameter("up_name"));
        contacts.setContacts_phone(httpServletRequest.getParameter("up_phone"));
        contacts.setContacts_email(httpServletRequest.getParameter("up_email"));
        boolean isUpdate = contactsService.updateSContacts(contacts);
        if (isUpdate) {
            System.out.println("==== Update Success\n==== " + contacts.toString());
        } else {
            System.out.println("==== Update Failure");
        }
        return new ModelAndView("redirect:/main.jsp");
    }
}
